package com.lxy.lambda;

import com.lxy.demo.A;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class ClassDemo {

    //通过集合创建流
    public static void t1() {
        List<String> list = Arrays.asList("AA", "BB", "CC");
        //通过集合获取stream流 - 串行流 - 正常运行 - 有顺序
        Stream<String> stream = list.stream();
        //forEach 打印输出，调用了forEach 自动关闭
        //stream.forEach(System.out::println);
        list.forEach(System.out::println);

        System.out.println("=====================");

        //并行流 - 速度快 - 不保证有序
        Stream<String> stringStream = list.parallelStream();
        stringStream.forEach(System.out::println);
        //串行流转换成并行流
        Stream<String> parallel = stream.parallel();
        parallel.forEach(System.out::println);
    }

    //通过数组创建流
    public static void t2() {
        //int类型数组
        int[] arr = new int[]{1, 2, 3, 4, 5, 6};
        //IntStream流
        IntStream stream = Arrays.stream(arr);
        stream.forEach(System.out::println);

        //对象类型
        Person[] peo = {
                new Person("张飞", 20),
                new Person("吕布", 10)
        };
        Stream<Person> stream1 = Arrays.stream(peo);
        stream1.forEach(System.out::println);
    }

    //通过Stream的of()方法，把可变参数列表转换为流
    public static void t3() {
        Stream<Person> personStream = Stream.of(
                new Person("张飞", 20),
                new Person("吕布", 10)
        );
        personStream.forEach(System.out::println);
    }

    //无限流

    //把集合中的偶数得到
    public static void t4() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        list.stream().filter(s -> s % 2 == 0).forEach(System.out::println);
        //limit(数量) 截取前()个
        list.stream().filter(s -> s % 2 == 0).limit(1).forEach(System.out::println);
        //skip(数量) 跳过前()个
        list.stream().filter(s -> s % 2 == 0).skip(1).forEach(System.out::println);
    }

    //元素去重distinct()
    public static void t5() {
        List<String> strings = Arrays.asList("AA", "BB", "CC", "AA", "BB", "CC", "AA", "BB", "CC");
        strings.stream().distinct().forEach(System.out::println);

        List<Person> people = Arrays.asList(
                new Person("张飞", 20),
                new Person("吕布", 10),
                new Person("张飞", 20),
                new Person("吕布", 10)
        );
        people.stream().distinct().forEach(System.out::println);
    }

    //Function接口  返回处理后的结果
    //转换大小写
    public static void t6() {
        List<String> list = Arrays.asList("a", "b", "c");
//        list.stream().map(new Function<String, Object>() {
//            @Override
//            public Object apply(String s) {
//                return s.toUpperCase();
//            }
//        }).forEach(System.out::print);

        //Stream + lambda
        //list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);

        //Stream + lambda + 引用
        list.stream().map(String::toUpperCase).forEach(System.out::println);
    }

    //只获取年龄大于15的人的名字
    public static void t7() {
        List<Person> list = Arrays.asList(
                new Person("张飞", 20),
                new Person("吕布", 10),
                new Person("马超", 20),
                new Person("小乔", 10)
        );

//        list.stream().filter(new Predicate<Person>() {
//            @Override
//            public boolean test(Person person) {
//                return person.getAge() > 15;
//            }
//        }).map(new Function<Person, Object>() {
//            @Override
//            public Object apply(Person person) {
//                return person.getName();
//            }
//        }).forEach(System.out::println);

        //Stream + lambda
        list.stream().filter(person -> person.getAge() > 15).map(person -> person.getName()).forEach(System.out::println);

        //Stream + lambda + 引用
        list.stream().filter(person -> person.getAge() > 15).map(Person::getName).forEach(System.out::println);
    }

    //集合排序
    public static void t8() {
        List<Integer> lists = new ArrayList<>();
        lists.add(10);
        lists.add(9);
        lists.add(1);
        lists.add(0);
        lists.add(3);
        lists.add(0);
        lists.add(8);
        lists.stream().sorted().forEach(System.out::println);
        System.out.println();
        //降序排序
        //Comparator.reverseOrder()返回一个和自然相反的比较器
        lists.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
    }

    //按照年龄从小到大排序，如果年龄一样，按照名字升序排序
    public static void t9() {
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 20),
                new Person("B", 70),
                new Person("G", 70)
        );
        list.stream().sorted(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge() == 0 ? o1.getName().compareTo(o2.getName()) : o1.getAge() - o2.getAge();
            }
        }).forEach(System.out::println);

        //lambda
        list.stream().sorted(((o1, o2) -> o1.getAge() - o2.getAge() == 0 ? o1.getName().compareTo(o2.getName()) : o1.getAge() - o2.getAge()));
    }

    //count查询满足条件的个数
    public static void t10() {
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 90),
                new Person("B", 70),
                new Person("G", 100)
        );
        System.out.println(list.stream().filter(Person -> Person.getAge() > 50).count());
    }

    //判断年龄是否都大于18岁
    public static void t11(){
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 90),
                new Person("B", 70),
                new Person("G", 100)
        );
        System.out.println(list.stream().allMatch(Person -> Person.getAge() > 18));
    }

    //是否存在年龄大于70的人
    public static void t13(){
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 90),
                new Person("B", 70),
                new Person("G", 100)
        );
        System.out.println(list.stream().anyMatch(Person -> Person.getAge() > 18));
    }

    //查询员工最高年龄
    public static void t12() {
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 90),
                new Person("B", 70),
                new Person("G", 100)
        );

        System.out.println("最大年龄：" + list.stream().max(((o1, o2) -> o1.getAge() - o2.getAge())).get().getAge());
        System.out.println("最小年龄：" + list.stream().min(((o1, o2) -> o1.getAge() - o2.getAge())).get().getAge());
    }

    //收集器 collect
    public static void t14(){
        List<Person> list = Arrays.asList(
                new Person("C", 20),
                new Person("A", 90),
                new Person("B", 70),
                new Person("G", 100)
        );
        List<String> collect = list.stream().filter(Person -> Person.getAge() > 50).map(Person::getName).collect(Collectors.toList());
        System.out.println(collect);
    }
    public static void main(String[] args) {
        t14();
    }
}
